//
//  AboutHealthManagerViewController.m
//  QinrenManagers
//
//  Created by easaa on 15/8/22.
//  Copyright (c) 2015年 easaa. All rights reserved.
//

#import "AboutHealthManagerViewController.h"

#import "VersionModel.h"

@interface AboutHealthManagerViewController ()<UIImagePickerControllerDelegate,UIActionSheetDelegate>
{
    //    NSDictionary    *audioDictionary;
    
    NSString        *lastVersion;//最新版本
}

//
@property (nonatomic, strong) NSMutableArray * versionArray;

@end

@implementation AboutHealthManagerViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        _versionArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"关于亲仁健康管家";
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    [self getVersionData];
    [self creatUI];
}

- (void)creatUI {

    UIImageView *iconView = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_W/2-50, 64+40, 100, 100)];
//    iconView.layer.cornerRadius = 50;
//    iconView.clipsToBounds = YES;
    [iconView setImage:[UIImage imageNamed:@"redLogo"]];
    
    UILabel *versionLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(iconView.frame)+10, SCREEN_W, 20)];
    
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    NSLog(@"系统版本的dictonary :%@？？？？？？？？",[infoDic description]);
    NSString *currentVersion = [infoDic objectForKey:@"CFBundleVersion"];
    NSLog(@"appVersion:%@==================",currentVersion);
    
    versionLabel.text = [NSString stringWithFormat:@"亲仁健康管家v_%@",currentVersion];
    versionLabel.textAlignment = NSTextAlignmentCenter;
    versionLabel.textColor = [UIColor darkGrayColor];
    
    UILabel *detailsLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, CGRectGetMaxY(versionLabel.frame), SCREEN_W-60, 200)];
    detailsLabel.textColor = [UIColor lightGrayColor];
    detailsLabel.numberOfLines = 0;
    detailsLabel.text = @"   有了健康管家APP，就可以管理所有的店面和客户，实现您手机管理所有工作的梦想！";
    
    
    UIButton * checkVersionButton = [[UIButton alloc]initWithFrame:CGRectMake(50, CGRectGetMaxY(detailsLabel.frame)-20, SCREEN_W - 100, 40)];
    
    [checkVersionButton setTitle:@"检测更新" forState:UIControlStateNormal];
    checkVersionButton.titleLabel.font = [UIFont systemFontOfSize:16];//title字体大小
    checkVersionButton.titleLabel.textAlignment = NSTextAlignmentCenter;//设置title的字体居中
    [checkVersionButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];//设置title在一般情况下为色字体
    [checkVersionButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];//设置title在button被选中情况下为灰色字体
    checkVersionButton.backgroundColor = Lightlightcolor;
    checkVersionButton.layer.cornerRadius = 5;
    checkVersionButton.clipsToBounds = YES;
    
    
    [checkVersionButton addTarget:self action:@selector(onCheckVersion) forControlEvents:UIControlEventTouchUpInside];

    

    [self.view addSubview:iconView];
    [self.view addSubview:versionLabel];
    [self.view addSubview:detailsLabel];
    
    [self.view addSubview:checkVersionButton];

}



- (void)onCheckVersion {
    
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    NSLog(@"系统版本的dictonary :%@？？？？？？？？",[infoDic description]);
    
    NSString *currentVersion = [infoDic objectForKey:@"CFBundleVersion"];
    NSLog(@"appVersion:%@==================",currentVersion);
    
    
    VersionModel *versionModel = _versionArray[0];
    
    lastVersion = versionModel.version_code;
    
    if (![lastVersion isEqualToString:currentVersion]) {
        //trackViewURL = [releaseInfo objectForKey:@"trackVireUrl"];
        //        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:[NSString  stringWithFormat:@"当前版本为v_%@\n有新的版本更新，是否前往更新？",currentVersion] delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:@"更新", nil];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:[NSString  stringWithFormat:@"当前版本为v_%@\n有新的版本更新，请前往AppStore更新",currentVersion] delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:nil, nil];
        alert.tag = 10000;
        [alert show];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:[NSString  stringWithFormat:@"当前版本为v_%@\n此版本为最新版本",currentVersion]delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        alert.tag = 10001;
        [alert show];
    }
    
}

#pragma mark - --------------获取版本更新网络请求
- (void)getVersionData {
    
    EAHttpAPIClient *manager = [EAHttpAPIClient manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    
    
    NSDictionary *parameters = @{@"type":@"4"};
    NSLog(@"%@",parameters);
    [manager GetWithParameters:parameters method:@"get.app.version" success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *resposeString ;
        if ([responseObject isKindOfClass:[NSData class]]) {
            resposeString = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
        }else
            resposeString = responseObject;
        
        NSString *jsonString = [DataParser parseString:resposeString];
        id dictionary = [JSONKit jsonToArrayOrDictionary:jsonString];
        NSLog(@"版本检测的dictonary :%@",[dictionary description]);
        
        
        if ([[dictionary objectForKey:@"verification"]intValue] == 1)
        {
            for (NSDictionary *dataDic in dictionary[@"data"]) {
                
                VersionModel * versionModel = [[VersionModel alloc]init];
                [versionModel setValuesForKeysWithDictionary:dataDic];
                
                [_versionArray addObject:versionModel];
                
            }
            
        }
        else
        {
            NSString *error = [dictionary objectForKey:@"error"];
            [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
            [SVProgressHUD showErrorWithStatus:error];
        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
        [SVProgressHUD showErrorWithStatus:[error localizedDescription]];
    }];
    
}



@end
